package com.project.web.controller.sys;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.hibernate.criterion.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.project.web.controller.BaseController;
import com.project.entity.sys.Role;
import com.project.service.sys.RoleService;
import com.project.interceptor.PreventDuplicatesToken;

/**
 * RoleController
 * @author weimingsen
 *
 */
@Controller
@RequestMapping("/role")
public class RoleController extends BaseController<Role>{
	
	private static final Logger logger = LoggerFactory.getLogger(RoleController.class);
	
	@Autowired
	private RoleService roleService;
	
		
	/**
	 * 系统角色列表
	 * @param model
	 * @param request
	 * @param page
	 * @return
	 */
	@RequestMapping(value="list")
	@RequiresPermissions("role:list")
	public String list(Model model,HttpServletRequest request,@ModelAttribute Role role){
		role.setcOrder(Order.desc("updateTime"));
		model.addAttribute("page", roleService.queryPage(role));
		model.addAttribute("role", role);
		
		return "/role/roleList";
	}
	
	/**
	 * 新增角色Init
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value="addInit")
	@RequiresPermissions("role:add")
	@PreventDuplicatesToken(save=true)
	public String addInit(Model model,HttpServletRequest request)throws Exception{
		String permissionsTree = roleService.queryPermissionsTree();
		
		model.addAttribute("permissionTree",permissionsTree);
		return "/role/addRole";
	}
	
	/**
	 * 新增用户
	 * @param model
	 * @param request
	 * @param role
	 * @return
	 */
	@RequestMapping(value="add")
	@RequiresPermissions("role:add")
	@PreventDuplicatesToken(remove=true)
	@ResponseBody
	public String add(Model model,HttpServletRequest request,@ModelAttribute Role role)throws Exception{
		String checkedIds = request.getParameter("checkedIds");
		roleService.save(role,checkedIds);
		return "true";
	}
	
	/**
	 * 修改系统角色Init
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value="updateInit")
	@RequiresPermissions("role:update")
	public String updateInit(Model model,HttpServletRequest request)throws Exception{
		String permissionsJson = roleService.getRolesPermissions(getEntity(),"update");
		
		model.addAttribute("permissionTree", permissionsJson);
		return "/role/updateRole";
	}
	
	/**
	 * 修改系统角色
	 * @param model
	 * @param request
	 * @param role
	 * @return
	 */
	@RequestMapping(value="update")
	@RequiresPermissions("role:update")
	@ResponseBody
	public String update(@ModelAttribute(value="entity") Role entity, Model model,HttpServletRequest request){
		String checkedIds = request.getParameter("checkedIds");
		roleService.update(entity,checkedIds);
		return "true";
	}
	
	/**
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="repetitionValidate")
	@ResponseBody
	public String repetitionValidate(HttpServletRequest request)throws Exception{
		String roleName = request.getParameter("roleName");
		String type = request.getParameter("status");
		String id = request.getParameter("updateId");
		
		String status = roleService.getStatus(roleName,type,id);
		return status;
	}

	/**
	 * 查看角色
	 * @param model
	 * @param request
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value="view")
	@RequiresPermissions("role:view")
	public String view(Model model,HttpServletRequest request) throws Exception{
		String permissionsJson = roleService.getRolesPermissions(getEntity(),"view");
		model.addAttribute("permissionTree", permissionsJson);
		
		return "/role/viewRole";
	}
	
	/**
	 * ajax验证角色是否删除
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="whetherCanDelete")
	@ResponseBody
	public String whetherCanDelete(HttpServletRequest request)throws Exception{
		String id = request.getParameter("id");
		
		boolean flag = roleService.whetherCanDelete(Long.parseLong(id));
		
		if(flag){
			return "true";
		}else{
			return "false";
		}
	}
	
	/**
	 * 删除
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="delete")
	@RequiresPermissions("role:delete")
	@ResponseBody
	public String delete(HttpServletRequest request)throws Exception{
		Role role = getEntity();
		roleService.delete(role);
		return "true";
	}
	
	/**
	 * 启用/禁用
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="doED")
	@RequiresPermissions("role:edRole")
	@ResponseBody
	public String doED(HttpServletRequest request)throws Exception{
		String status = request.getParameter("status");
		Role role = getEntity();
		
		roleService.doEd(role, status);
		
		return "true";
	}
	
	
	
	@Override
	protected Class<Role> getClazz() {
		return Role.class;
	}
	
}
